package com.test.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.DbHelp;
import com.test.model.TableBean;

/**
 * Servlet implementation class DbServlet
 */
public class DbServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DbServlet() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		
			  	List<TableBean> list = DbHelp.getDbHelp().getTables(); 
			  	request.setAttribute("list", list);
			 // 	response.sendRedirect("data.jsp");
			  	RequestDispatcher dispatcher = request.getRequestDispatcher("data.jsp");
			  	dispatcher .forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 this.doGet(request, response);
	}
	
	/*public static void main(String[] args) {
		DataSource datasource = DbHelp.getDbHelp().initDataSource();
		try {
			Connection conn = datasource.getConnection();
			
			//获取所有表名
			ResultSet rs = conn.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
				List<String> tableName=new ArrayList<String>();
				while(rs.next()){
					 System.out.print("目录名："+rs.getString(1));
				      System.out.print(" 模式名："+rs.getString(2));
				      System.out.print(" 表名："+rs.getString(3));
				      System.out.print(" 表的类型："+rs.getString(4));
				      System.out.println(" 注释："+rs.getString(5));
				      tableName.add(rs.getString(3));
			 	} 
			
			 	List<TableBean> tabbeanList=new ArrayList<TableBean>();
			for(String name:tableName){
				TableBean tab=new TableBean(name);
				List<String> col=new ArrayList<String>();
				ResultSet res = conn.getMetaData().getColumns(null, null,name, null);
				while(res.next()){
					col.add(res.getString(4));
					System.out.println("表名      "+res.getString(3));		 
					System.out.println("列名      "+res.getString(4));	
					//System.out.println("类型名      "+res.getString(5));	
				}
				tab.setColumn(col);
				tabbeanList.add(tab);
			}
			
			conn.close();
			for(TableBean b:tabbeanList){
				System.out.println(b);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

}
